/*
 * KeyEdit.java
 *
 * Created on 22. listopad 2002, 15:58
 */

package cz.masa.FotoCAT;

import java.sql.*;
import javax.swing.*;
import java.util.*;

/**
 *
 * @author Jiri Masnik
 */
public class KeyEdit extends javax.swing.JInternalFrame {
    
    /** Creates new form KeyEdit */
    public KeyEdit() {
        setLocation(20,20);
        initComponents();
        refreshList();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        buttonGroup1 = new javax.swing.ButtonGroup();
        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        downPanel = new javax.swing.JPanel();
        butPanel = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        topPanel = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Keywords");
        setMinimumSize(new java.awt.Dimension(300, 200));
        setPreferredSize(new java.awt.Dimension(310, 333));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                onClose(evt);
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        mainPanel.setLayout(new java.awt.BorderLayout());

        mainPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(5, 15, 5, 15)));
        mainPanel.setMinimumSize(new java.awt.Dimension(400, 300));
        mainPanel.setPreferredSize(new java.awt.Dimension(400, 300));
        jScrollPane1.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(3, 2, 8, 2)));
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                onListSel(evt);
            }
        });
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                onListClick(evt);
            }
        });

        jScrollPane1.setViewportView(jList1);

        mainPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        downPanel.setLayout(new java.awt.BorderLayout());

        jButton1.setText("Add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onAdd(evt);
            }
        });

        butPanel.add(jButton1);

        jButton3.setText("Edit");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onEdit(evt);
            }
        });

        butPanel.add(jButton3);

        jButton2.setText("Delete");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onDelete(evt);
            }
        });

        butPanel.add(jButton2);

        downPanel.add(butPanel, java.awt.BorderLayout.SOUTH);

        mainPanel.add(downPanel, java.awt.BorderLayout.SOUTH);

        jRadioButton1.setSelected(true);
        jRadioButton1.setText("keywords");
        buttonGroup1.add(jRadioButton1);
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioKeywords(evt);
            }
        });

        topPanel.add(jRadioButton1);

        jRadioButton2.setText("attributes");
        buttonGroup1.add(jRadioButton2);
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioAttrib(evt);
            }
        });

        topPanel.add(jRadioButton2);

        mainPanel.add(topPanel, java.awt.BorderLayout.NORTH);

        getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void onEdit(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onEdit
        idtext idt = (idtext)jList1.getSelectedValue();
        if(idt == null) return; 
        JOptionPane dlg = new JOptionPane();
        String ans = (String)dlg.showInternalInputDialog(FotoCAT.desktop, "Type new value of keyword '"+idt.text+"'","Edit keyword",JOptionPane.QUESTION_MESSAGE,null,null,idt.text);
        if(ans == null) return;
        if(ans.compareTo("")!=0) SQLCon.exSQL("UPDATE fca05_keyword SET fca05_value = '" + ans + "' WHERE fca05_id= " + idt.id);
        refreshList();
    }//GEN-LAST:event_onEdit

    private void radioAttrib(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioAttrib
        refreshList();
    }//GEN-LAST:event_radioAttrib

    private void radioKeywords(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioKeywords
        refreshList();
    }//GEN-LAST:event_radioKeywords

    private void onListClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_onListClick
        // Add your handling code here:
        if(evt.getClickCount()>1){
            onEdit(null);
        }
    }//GEN-LAST:event_onListClick

    private void onListSel(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_onListSel
        idtext idt = (idtext)jList1.getSelectedValue();
        //jTextField2.setText(idt.text);
    }//GEN-LAST:event_onListSel

    private void onClose(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_onClose
        // Add your handling code here:
    }//GEN-LAST:event_onClose

    private void onDelete(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onDelete
        idtext idt = (idtext)jList1.getSelectedValue();
        if(idt == null) return;
        int pocet = 1;
        ResultSet rs = SQLCon.exSQLres("SELECT COUNT(*) AS pocet FROM fca06_picture_keyword WHERE fca05_id = " + idt.id);
            try {
                rs.next();
		pocet = rs.getInt("pocet");
            } catch(SQLException ex) { return; }
            if(pocet > 0){
                JOptionPane.showInternalMessageDialog(FotoCAT.desktop,"Can't delete this keyword. Some picture have it assigned.","Error",JOptionPane.ERROR_MESSAGE);
                return;
            }
            SQLCon.exSQL("DELETE FROM fca05_keyword WHERE fca05_id = " + idt.id);
            refreshList();
    }//GEN-LAST:event_onDelete

    private void onAdd(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onAdd
        JOptionPane dlg = new JOptionPane();
        String ans = (String)dlg.showInternalInputDialog(FotoCAT.desktop, "Type new keyword","New keyword",JOptionPane.QUESTION_MESSAGE,null,null,"");
        if(ans == null) return;
        if(ans.compareTo("")!=0) SQLCon.exSQL("INSERT INTO fca05_keyword(fca05_value, fca05_type) VALUES ('"+ans+"'," + getCurentType() + ")");
	refreshList();
    }//GEN-LAST:event_onAdd
    
    public int getCurentType(){
        if(jRadioButton1.isSelected()) return 0;
        if(jRadioButton2.isSelected()) return 1;
        return 0;
    }
    
    public void refreshList(){
        jList1.setListData(KeyEdit.allKeywords(getCurentType()));
    }
    
    public static Vector allKeywords(int type){
        Vector vect = new Vector();
        ResultSet rs = SQLCon.exSQLres("SELECT fca05_id AS id, fca05_value AS key_str FROM fca05_keyword WHERE fca05_type = " + type + " ORDER BY key_str");
        try {
            while(rs.next()){
                int idrs = rs.getInt("id");
                String nazevrs = rs.getString("key_str");
                vect.add(new idtext(idrs,nazevrs));
            }
        } catch(SQLException ex) {  }
        return vect;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel butPanel;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel downPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JList jList1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables
    
}
